import * as Cesium from 'cesium';

export class TrajectoryTools {
  constructor(cesiumManager) {
    this.cesiumManager = cesiumManager;
    this.tempEntities = [];
    this.trajectoryPaths = new Map();
    this.isPlaying = false;
    this.speed = 1.0;
  }

  // 添加历史轨迹
  addHistoricalTrajectory() {
    if (!this.cesiumManager || !this.cesiumManager.viewer) return;
    
    const viewer = this.cesiumManager.viewer;
    
    // 示例轨迹数据 - 长沙附近的一条轨迹
    const positions = [];
    const centerLon = 112.98;
    const centerLat = 28.19;
    
    for (let i = 0; i < 20; i++) {
      const lon = centerLon + (Math.random() - 0.5) * 0.1;
      const lat = centerLat + (Math.random() - 0.5) * 0.1;
      positions.push(Cesium.Cartesian3.fromDegrees(lon, lat));
    }
    
    // 创建轨迹线
    const pathEntity = viewer.entities.add({
      polyline: {
        positions: positions,
        width: 3,
        material: new Cesium.PolylineGlowMaterialProperty({
          color: Cesium.Color.CYAN,
          glowPower: 0.1
        })
      }
    });
    
    this.tempEntities.push(pathEntity);
    
    // 创建移动点
    const movingPoint = viewer.entities.add({
      position: new Cesium.CallbackProperty(() => {
        // 简化的动画效果
        const timeIndex = Math.floor((Date.now() / 1000) % positions.length);
        return positions[timeIndex];
      }, false),
      point: {
        pixelSize: 10,
        color: Cesium.Color.RED,
        outlineColor: Cesium.Color.WHITE,
        outlineWidth: 2
      }
    });
    
    this.tempEntities.push(movingPoint);
    
    // 保存轨迹信息
    this.trajectoryPaths.set('default', {
      positions: positions,
      pathEntity: pathEntity,
      movingPoint: movingPoint
    });
    
    console.log('添加历史轨迹');
  }

  // 网格与人虚线连接
  addGridPersonConnection() {
    if (!this.cesiumManager || !this.cesiumManager.viewer) return;
    
    const viewer = this.cesiumManager.viewer;
    
    // 创建网格点
    const gridPoint = viewer.entities.add({
      position: Cesium.Cartesian3.fromDegrees(112.98, 28.19),
      point: {
        pixelSize: 12,
        color: Cesium.Color.BLUE,
        outlineColor: Cesium.Color.WHITE,
        outlineWidth: 2
      },
      label: {
        text: '网格点',
        font: '14px sans-serif',
        fillColor: Cesium.Color.WHITE,
        outlineColor: Cesium.Color.BLACK,
        outlineWidth: 2,
        style: Cesium.LabelStyle.FILL_AND_OUTLINE,
        verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
        pixelOffset: new Cesium.Cartesian2(0, -20)
      }
    });
    
    this.tempEntities.push(gridPoint);
    
    // 创建人员点
    const personPoint = viewer.entities.add({
      position: Cesium.Cartesian3.fromDegrees(112.99, 28.20),
      point: {
        pixelSize: 12,
        color: Cesium.Color.YELLOW,
        outlineColor: Cesium.Color.WHITE,
        outlineWidth: 2
      },
      label: {
        text: '人员',
        font: '14px sans-serif',
        fillColor: Cesium.Color.WHITE,
        outlineColor: Cesium.Color.BLACK,
        outlineWidth: 2,
        style: Cesium.LabelStyle.FILL_AND_OUTLINE,
        verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
        pixelOffset: new Cesium.Cartesian2(0, -20)
      }
    });
    
    this.tempEntities.push(personPoint);
    
    // 创建连接线
    const connectionLine = viewer.entities.add({
      polyline: {
        positions: new Cesium.CallbackProperty(() => {
          const gridPos = gridPoint.position.getValue(Cesium.JulianDate.now());
          const personPos = personPoint.position.getValue(Cesium.JulianDate.now());
          if (gridPos && personPos) {
            return [gridPos, personPos];
          }
          return [];
        }, false),
        width: 2,
        material: new Cesium.PolylineDashMaterialProperty({
          color: Cesium.Color.WHITE
        })
      }
    });
    
    this.tempEntities.push(connectionLine);
    
    console.log('添加网格与人员虚线连接');
  }

  // 带model历史轨迹
  addModelTrajectory() {
    if (!this.cesiumManager || !this.cesiumManager.viewer) return;
    
    const viewer = this.cesiumManager.viewer;
    
    // 示例轨迹数据
    const positions = [];
    const centerLon = 112.98;
    const centerLat = 28.19;
    
    for (let i = 0; i < 15; i++) {
      const lon = centerLon + (Math.random() - 0.5) * 0.05;
      const lat = centerLat + (Math.random() - 0.5) * 0.05;
      positions.push(Cesium.Cartesian3.fromDegrees(lon, lat));
    }
    
    // 创建轨迹线
    const pathEntity = viewer.entities.add({
      polyline: {
        positions: positions,
        width: 2,
        material: Cesium.Color.YELLOW
      }
    });
    
    this.tempEntities.push(pathEntity);
    
    // 创建模型点（使用简单的box作为示例）
    const modelEntity = viewer.entities.add({
      position: new Cesium.CallbackProperty(() => {
        // 简化的动画效果
        const timeIndex = Math.floor((Date.now() / 1000) % positions.length);
        return positions[timeIndex];
      }, false),
      box: {
        dimensions: new Cesium.Cartesian3(10000.0, 10000.0, 10000.0),
        material: Cesium.Color.RED.withAlpha(0.5)
      }
    });
    
    this.tempEntities.push(modelEntity);
    
    console.log('添加带模型的历史轨迹');
  }

  // 播放轨迹
  play() {
    this.isPlaying = true;
    console.log('开始播放轨迹');
  }

  // 暂停轨迹
  pause() {
    this.isPlaying = false;
    console.log('暂停播放轨迹');
  }

  // 加速播放
  speedUp() {
    this.speed = Math.min(this.speed + 0.5, 5.0);
    console.log('加速播放，当前速度:', this.speed);
  }

  // 减速播放
  speedDown() {
    this.speed = Math.max(this.speed - 0.5, 0.5);
    console.log('减速播放，当前速度:', this.speed);
  }

  getTempEntities() {
    return this.tempEntities;
  }

  setTempEntities(entities) {
    this.tempEntities = entities;
  }

  clear() {
    if (!this.cesiumManager || !this.cesiumManager.viewer) return;
    
    const viewer = this.cesiumManager.viewer;
    
    this.tempEntities.forEach(entity => {
      viewer.entities.remove(entity);
    });
    
    this.tempEntities = [];
    this.trajectoryPaths.clear();
    this.isPlaying = false;
    this.speed = 1.0;
  }
}